<template>
    <table>
        <tr>
            <td>房型名称</td>
            <td>
                <input type="text" v-model="poks.name">
            </td>
        </tr>
        <tr>
            <td>房型图片</td>
            <td>
                <input type="file" @change="imahomskw">
                <img :src="src" width="60" height="60" v-if="poks.picture">
            </td>
        </tr>
        <tr>
            <td></td>
            <td><input type="button" value="添加" @click="adduwonmw"></td>
        </tr>
    </table>
  </template>
  
  <script setup lang="ts">
  import {reactive, toRefs} from 'vue'
  import {useRouter,useRoute} from 'vue-router'
  import axios from 'axios'
  const router=useRouter()
  const route=useRoute()

  let data=reactive({
    poks:{
        name:'',
        picture:''
    },
    src:''
  })
  let {poks,src} =toRefs(data)

  const imahomskw=(e:any)=>{
    var form=new FormData()
    var obj=e.target.files[0]
    form.append('file',obj)
    axios({
        url:'/api/Upfile/Upload',
        method:'post',
        data:form
    })
    .then(res=>{
        if(res.data.code==11){
            alert(res.data.ms)
            return;
        }else if(res.data.code==12){
            alert(res.data.ms)
            return;
        }else{
            src.value='https://localhost:7246'+res.data.filename;
            poks.value.picture=res.data.filename;
        }
    })
  }

  const adduwonmw=()=>{
    let obj={
        "rname": poks.value.name,
  "rpicture": poks.value.picture
    }
    axios({
        url:'/api/Price/Addtype',
        method:'post',
        data:obj
    })
    .then(res=>{
        if(res.data.data==1){
            alert("添加成功")
        }else{
            alert("添加失败")
        }
    })
  }
  </script>
  
  <style scoped>
  
  </style>